﻿using System;
using System.IO;
using System.Diagnostics;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BrainTechLLC.FileMaintenance
{
    public class Program
    {
        static void Main(string[] args)
        {
            string startingDirectory = string.Empty;

            FileWorker worker = new FileWorker();
            worker.DeletedDirectory += new EventHandler<ObjectEventArgs<DirectoryInfo>>(worker_DeletedDirectory);
            worker.DeletedFile += new EventHandler<ObjectEventArgs<FileInfo>>(worker_DeletedFile);

            while (string.IsNullOrEmpty(startingDirectory) || !Directory.Exists(startingDirectory))
            {
                Console.Write("Enter starting (root) directory or Q to quit: ");
                startingDirectory = Console.ReadLine();

                if (startingDirectory.ToUpper() == "Q")
                    return;

                if (!Directory.Exists(startingDirectory))
                {
                    Console.WriteLine("Directory does not exist");
                }
            }

            Options options = Options.DoublePass | Options.ContinueOnException;
            bool? deletePSESS = AskUser("Delete .psess files?");

            if (deletePSESS == null)
                return;

            if (deletePSESS == true)
                options |= Options.DeletePSESS;

            bool? removeReadOnly = AskUser("Remove read-only flags before delete?");

            if (removeReadOnly == null)
                return;

            if (removeReadOnly == true)
                options |= Options.RemoveReadOnlyFlags;

            bool? deleteSVN = AskUser("Delete .svn directories and files?");

            if (deleteSVN == null)
                return;

            if (deleteSVN == true)
                options |= Options.DeleteSVN;

            double? minSize = AskUserNumber("Minimum size (in MB) to consider for deletion (0 = do not delete by size)?");

            if (minSize == null)
                return;

            double? maxSize = 0D;

            if (minSize != 0D)
            {
                maxSize = AskUserNumber("Max size (in MB) to consider for deletion (0 = no size limit)?");

                if (maxSize == null)
                    return;
            }
            worker.StartNukeBinObjs(startingDirectory, null, (double)minSize, (double)maxSize, options, 
                (fi) => ReadResponse(fi), (di) => ReadResponseDirectory(di),
                (ex) => Console.WriteLine(ex.ToString()));
        }

        static double? AskUserNumber(string question)
        {
            double d = double.NegativeInfinity;

            while (d == double.NegativeInfinity)
            {
                Console.Write(question);
                string answerString = Console.ReadLine();

                if (answerString.ToUpper().StartsWith("Q"))
                    return null;

                if (double.TryParse(answerString, out d))
                    return d;
            }

            return d;
        }

        static bool? AskUser(string question)
        {
            Console.Write(question);
            string answerString = Console.ReadLine();

            if (answerString.ToUpper().StartsWith("Y"))
                return true;

            if (answerString.ToUpper().StartsWith("Q"))
                return null;

            return false;
        }

        static void worker_DeletedFile(object sender, ObjectEventArgs<FileInfo> e)
        {
            //Console.WriteLine("Deleted file " + e.Item.FullName);
        }

        static void worker_DeletedDirectory(object sender, ObjectEventArgs<DirectoryInfo> e)
        {
            //Console.WriteLine("Deleted directory " + e.Item.FullName);
        }

        public static Response ResponseFromKey()
        {
            var v = Console.ReadKey();
            Console.WriteLine();

            if (v.KeyChar != 'y')
            {
                if (v.KeyChar == 'Y')
                {
                    return Response.CapitalYes;
                }

                if (v.KeyChar == 'N')
                {
                    return Response.CapitalNo;
                }

                return Response.No;
            }

            return Response.Yes;
        }

        public static Response ReadResponse(FileInfo fi)
        {
            Console.Write("Size: " + (fi.Length / (1024 * 1024)).ToString() + "MB, File " + fi.FullName + ", Delete?");
            return ResponseFromKey();
        }

        public static Response ReadResponseDirectory(DirectoryInfo di)
        {
            Console.Write("Delete directory " + di.FullName + "?");
            return ResponseFromKey();
        }
    }
}
